HAL SDRAM initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 SDRAM configuration panel.

  • Configure the SW layer HAL.

  • Configure the SDRAM main features.

  • Configure the SDRAM advanced features.

  • Configure the SDRAM security and privilege features if needed.

  • Configure the SDRAM GPIO pins.

  • Configure the NVIC if the application requires the SDRAM to be used in IT mode.

  • Generate the code.

HAL SDRAM configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

HAL SDRAM main features configuration

Configure the SDRAM main features.

SDRAM main feature configuration

HAL SDRAM advanced features configuration

Configure the advanced SDRAM features.

SDRAM advanced feature configuration

HAL SDRAM security and privilege features configuration

Configure the SDRAM security and privilege features if needed.

SDRAM security and privilege feature configuration

GPIO configuration

Configure GPIO pins.

SDRAM GPIO configuration

NVIC configuration

Configure the NVIC in case of an interrupt.

SDRAM NVIC configuration

Generated SDRAM initialization and de-initialization sequences

The SDRAM initialization sequence is generated in the mx_fmc_sdram_init function in mx_fmc.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated : SDRAM initialization and De-initialization sequence

Topic

HAL1 Code Snippet

HAL2 Code Snippet

Handle declaration

SDRAM_HandleTypeDef hsdram;

GPIO_InitTypeDef gpio_init_structure;
hal_sdram_common_config_t sdram_config;
hal_sdram_memory_config_t p_config1;
hal_gpio_config_t  gpio_config;

Initialization and configuration sequence

hsdram.Instance = FMC_SDRAM_DEVICE;

/* Timing configuration  */
SDRAM_Timing.LoadToActiveDelay    = 2;
SDRAM_Timing.ExitSelfRefreshDelay = 7;
SDRAM_Timing.SelfRefreshTime      = 4;
SDRAM_Timing.RowCycleDelay        = 7;
SDRAM_Timing.WriteRecoveryTime    = 3;
SDRAM_Timing.RPDelay              = 2;
SDRAM_Timing.RCDDelay             = 2;

hsdram.Init.SDBank             = FMC_SDRAM_BANK2;
hsdram.Init.ColumnBitsNumber   = FMC_SDRAM_COLUMN_BITS_NUM_9;
hsdram.Init.RowBitsNumber      = FMC_SDRAM_ROW_BITS_NUM_11;
hsdram.Init.MemoryDataWidth    = SDRAM_MEMORY_WIDTH;
hsdram.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
hsdram.Init.CASLatency         = FMC_SDRAM_CAS_LATENCY_3;
hsdram.Init.WriteProtection    = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
hsdram.Init.SDClockPeriod      = SDCLOCK_PERIOD;
hsdram.Init.ReadBurst          = FMC_SDRAM_RBURST_ENABLE;
hsdram.Init.ReadPipeDelay      = FMC_SDRAM_RPIPE_DELAY_2;

/* Initialize the SDRAM controller */
if(HAL_SDRAM_Init(&hsdram, &SDRAM_Timing) != HAL_OK)
{
  /* Initialization Error */
  Error_Handler();
}

void HAL_SDRAM_MspInit(SDRAM_HandleTypeDef *hsdram)
{

    /* Enable FMC clock */
  __HAL_RCC_FMC_CLK_ENABLE();

  /* Enable GPIOs clock */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOG_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOI_CLK_ENABLE();

  /* Common GPIO configuration */
  gpio_init_structure.Mode      = GPIO_MODE_AF_PP;
  gpio_init_structure.Pull      = GPIO_PULLUP;
  gpio_init_structure.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
  gpio_init_structure.Alternate = GPIO_AF12_FMC;

  /* GPIOA configuration */
  gpio_init_structure.Pin   = GPIO_PIN_7;
  HAL_GPIO_Init(GPIOA, &gpio_init_structure);

  /* GPIOD configuration */
  gpio_init_structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_8| GPIO_PIN_9 | \
                              GPIO_PIN_10 | GPIO_PIN_14 | GPIO_PIN_15;

  HAL_GPIO_Init(GPIOD, &gpio_init_structure);

  /* GPIOE configuration */
  gpio_init_structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_7| GPIO_PIN_8 | \
                              GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;

  HAL_GPIO_Init(GPIOE, &gpio_init_structure);

  /* GPIOF configuration */
  gpio_init_structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | \
                              GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;

  HAL_GPIO_Init(GPIOF, &gpio_init_structure);

  /* GPIOG configuration */
  gpio_init_structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_8 | GPIO_PIN_15;

  HAL_GPIO_Init(GPIOG, &gpio_init_structure);

  /* GPIOH configuration */
  gpio_init_structure.Pin   = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | \
                              GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;

  HAL_GPIO_Init(GPIOH, &gpio_init_structure);

  /* GPIOI configuration */
  gpio_init_structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | \
                              GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_9 | GPIO_PIN_10;

  HAL_GPIO_Init(GPIOI, &gpio_init_structure);

}
hal_sdram_handle_t *mx_fmc_sdram_init(void)
{
  hal_sdram_common_config_t sdram_config;

  if (HAL_SDRAM_Init(&hSDRAM, HAL_SDRAM) != HAL_OK)
  {
    return NULL;
  }

  HAL_RCC_FMC_EnableClock();

  sdram_config.read_pipe_delay                   = HAL_SDRAM_READ_PIPE_DELAY_0_CYCLE;
  sdram_config.sdram_clock_divider               = HAL_SDRAM_FMC_KER_CK_DIV_1;
  sdram_config.load_to_active_delay_nsec         = 20;
  sdram_config.exit_self_refresh_delay_nsec      = 70;
  sdram_config.self_refresh_time_nsec            = 40;
  sdram_config.row_cycle_delay_nsec              = 70;
  sdram_config.write_recovery_time_nsec          = 30;
  sdram_config.precharge_command_delay_nsec      = 20;
  sdram_config.activate_rd_wr_command_delay_nsec = 20;
  sdram_config.rows_number                       = 11;

  if (HAL_SDRAM_SetConfig(&hSDRAM, &sdram_config) != HAL_OK)
  {
    return NULL;
  }

  /* Memory configuration */

  p_config1.memory_bus_width     = HAL_SDRAM_MEMORY_BUS_WIDTH_8_BIT;
  p_config1.intern_banks_number  = HAL_SDRAM_INTERN_BANKS_NUMBER_2;
  p_config1.row_address_width    = HAL_SDRAM_ROW_ADDR_WIDTH_11_BIT;
  p_config1.column_address_width = HAL_SDRAM_COLUMN_ADDR_WIDTH_8_BIT;
  p_config1.cas_latency          = HAL_SDRAM_CAS_LATENCY_1_CYCLE;

  if (HAL_SDRAM_SetConfigMemory(&hSDRAM, HAL_SDRAM_DEVICE2, &p_config1) != HAL_OK)
  {
    return NULL;
  }

  /* Enable the interruption for SDRAM */
  HAL_CORTEX_NVIC_SetPriority(FMC_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(FMC_IRQn);

  HAL_RCC_GPIOC_EnableClock();

  HAL_RCC_GPIOD_EnableClock();

  HAL_RCC_GPIOE_EnableClock();

  HAL_RCC_GPIOA_EnableClock();

  HAL_RCC_GPIOB_EnableClock();

  HAL_RCC_GPIOJ_EnableClock();


  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_12;
  HAL_GPIO_Init(HAL_GPIOC, HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_3 | HAL_GPIO_PIN_4 | HAL_GPIO_PIN_5 | HAL_GPIO_PIN_8, &gpio_config);

  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_12;
  HAL_GPIO_Init(HAL_GPIOD, HAL_GPIO_PIN_14 | HAL_GPIO_PIN_15 | HAL_GPIO_PIN_12 | HAL_GPIO_PIN_11 | HAL_GPIO_PIN_13, &gpio_config);

  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_12;
  HAL_GPIO_Init(HAL_GPIOE, HAL_GPIO_PIN_8 | HAL_GPIO_PIN_11, &gpio_config);

  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_10;
  HAL_GPIO_Init(HAL_GPIOD, HAL_GPIO_PIN_8 | HAL_GPIO_PIN_9, &gpio_config);

  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_14;
  HAL_GPIO_Init(HAL_GPIOE, HAL_GPIO_PIN_9 | HAL_GPIO_PIN_10, &gpio_config);

  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_12;
  HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_12 | HAL_GPIO_PIN_11 | HAL_GPIO_PIN_10 | HAL_GPIO_PIN_9 | \
                           HAL_GPIO_PIN_2 | HAL_GPIO_PIN_1 | HAL_GPIO_PIN_0, &gpio_config);

  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_10;
  HAL_GPIO_Init(HAL_GPIOB, HAL_GPIO_PIN_0, &gpio_config);

    /* Enable the VddXSPI2 independent supply */
  if (HAL_PWR_EnableVddXSPI2IndependentSupply() != HAL_OK)
  {
    return NULL;
  }

  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_14;
  HAL_GPIO_Init(HAL_GPIOJ, HAL_GPIO_PIN_7 | HAL_GPIO_PIN_6, &gpio_config);

  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_7;
  HAL_GPIO_Init(HAL_GPIOC, HAL_GPIO_PIN_9, &gpio_config);

  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_12;
  HAL_GPIO_Init(HAL_GPIOB, HAL_GPIO_PIN_7 | HAL_GPIO_PIN_6, &gpio_config);

  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_7;
  HAL_GPIO_Init(HAL_GPIOD, HAL_GPIO_PIN_2, &gpio_config);

  return &hSDRAM;
}

Deinitialization sequence

void HAL_SDRAM_MspDeInit(SDRAM_HandleTypeDef *hsdram)
{
  /*## Disable peripherals and GPIO Clocks ###################################*/
  /* Configure FMC as alternate function  */
  HAL_GPIO_DeInit(GPIOA, GPIO_PIN_7);

  HAL_GPIO_DeInit(GPIOB, GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);

  HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5);

  HAL_GPIO_DeInit(GPIOD, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | \
                         GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_14 | GPIO_PIN_15);

  HAL_GPIO_DeInit(GPIOE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3| GPIO_PIN_4 | \
                         GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);

  HAL_GPIO_DeInit(GPIOF, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | GPIO_PIN_4 | \
                         GPIO_PIN_5 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);

  HAL_GPIO_DeInit(GPIOG, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | \
                         GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 |  GPIO_PIN_14 | GPIO_PIN_15);

  HAL_GPIO_DeInit(GPIOH, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | \
                         GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15);

  HAL_GPIO_DeInit(GPIOH, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_9 | GPIO_PIN_10);
}
void mx_fmc_sdram_deinit(void)
{
  /* Disable the interruption for SDRAM */
  HAL_CORTEX_NVIC_DisableIRQ(FMC_IRQn);

  /* De-initialize all GPIO pins associated with FMC */
  HAL_GPIO_DeInit(HAL_GPIOC, HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_3 | HAL_GPIO_PIN_4 | HAL_GPIO_PIN_5 | HAL_GPIO_PIN_8 | HAL_GPIO_PIN_9);

  HAL_GPIO_DeInit(HAL_GPIOD, HAL_GPIO_PIN_2 | HAL_GPIO_PIN_8 | HAL_GPIO_PIN_9 | HAL_GPIO_PIN_11 | HAL_GPIO_PIN_12 | HAL_GPIO_PIN_13 | HAL_GPIO_PIN_14 | HAL_GPIO_PIN_15);

  HAL_GPIO_DeInit(HAL_GPIOE, HAL_GPIO_PIN_8 | HAL_GPIO_PIN_9 | HAL_GPIO_PIN_10 | HAL_GPIO_PIN_11);

  HAL_GPIO_DeInit(HAL_GPIOA, HAL_GPIO_PIN_0 | HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_9 | HAL_GPIO_PIN_10 | HAL_GPIO_PIN_11 | HAL_GPIO_PIN_12);

  HAL_GPIO_DeInit(HAL_GPIOB, HAL_GPIO_PIN_0 | HAL_GPIO_PIN_6 | HAL_GPIO_PIN_7);

  HAL_GPIO_DeInit(HAL_GPIOJ, HAL_GPIO_PIN_6 | HAL_GPIO_PIN_7);

  HAL_SDRAM_DeInit(&hSDRAM);
}
STM32CubeMX2 Generated : IRQ handle function

HAL1 / HAL2 Code Snippet

void FMC_IRQHandler(void)
{
  HAL_SDRAM_IRQHandler(&hSDRAM);
}